package com.kele.controller;

import com.kele.domain.Blog;
import com.kele.domain.Comment;
import com.kele.domain.CommentSon;
import com.kele.domain.User;
import com.kele.service.BlogService;
import com.kele.service.CommentService;
import com.kele.service.CommentSonService;
import com.kele.service.UserService;
import com.kele.utils.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 栗守佳
 */
@Controller
public class CommentSonController {
    @Resource
    CommentSonService commentSonService;
    @Resource
    BlogService blogService;
    @Resource
    UserService userService;
    @Resource
    CommentService commentService;

    /**
     * 寻找子评论
     * @author 栗守佳
     * @param blogid
     * @param commentid
     * @param bloggerid
     * @param userid
     * @return
     */
    @RequestMapping(value = "/findCommentSon", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData findCommentSon(int blogid, int commentid, int bloggerid, int userid) {
        Blog blog = null;
        User user1 = null;
        User user2 = null;
        blog = blogService.findBlogByBid(blogid);
        user1 = userService.queryUserById(userid);
        user2 = userService.queryUserById(bloggerid);
        List<CommentSon> cs = commentSonService.findCommentSon(blog.getBid(), user1.getId(), user2.getId(), commentid);

        if (cs.size() != 0) {
            return ResponseData.success("查询成功", cs);
        } else {
            return ResponseData.fail("查询失败");
        }

    }


    /**
     * 用户进行回复,子评论的发布
     *
     * @author 栗守佳
     * @param blogid
     * @param commentid
     * @param bloggerid
     * @param userid
     * @param content
     * @return
     */
    @RequestMapping(value = "/insertCommentSon", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData insertCommentSon(int blogid, int commentid, int bloggerid, int userid, String content) {
        Blog blog = null;
        User user1 = null;
        User user2 = null;
        CommentSon commentSon = new CommentSon();
        blog = blogService.findBlogByBid(blogid);
        user1 = userService.queryUserById(userid);
        user2 = userService.queryUserById(bloggerid);
        commentSon.setUserid(user1.getId());
        commentSon.setUsername(user1.getNickname());
        commentSon.setBloggerid(user2.getId());
        commentSon.setBloggername(user2.getNickname());
        commentSon.setBlogid(blog.getBid());
        commentSon.setBlogname(blog.getTitle());
        commentSon.setContent(content);
        commentSon.setCommentid(commentid);
        int count = commentSonService.insertCommentSon(commentSon);
        if (count > 0) {
            return ResponseData.success("回复成功", count);
        }
        return ResponseData.fail("回复失败");
    }

    /**
     * 删除子评论
     * @author 栗守佳
     * @param commentSonid
     * @return
     */
    @RequestMapping(value = "/deleteCommentSon", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData deleteCommentSon(int commentSonid) {
        //删除子评论
        int count = commentSonService.deleteCommentSon(commentSonid);
        return ResponseData.success("主评论删除成功", count);
    }



    @RequestMapping(value = "/findCommentSons",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData findCommentSons(int commentid){
        List<CommentSon> cs= commentSonService.findCommentSons(commentid);
        System.out.println(cs);
        if(cs.size()!=0){
            return ResponseData.success("查询成功",cs);
        }else{
            return ResponseData.fail("查询失败");
        }

    }
}







































